﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using Cms.Web.UI.Admin.HelperClasses;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class EditMembership : AdminPage, IEditMembershipView
    {
        private EditMembershipPresenter _presenter;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new EditMembershipPresenter(this);
            HeadingText = GetText(this, MembershipID > 0 ? "EditMembershipHeading" : "NewMembershipHeading");
            if (Page.IsPostBack)
                return;
            _presenter.Initialize();
            HeadingIcon = litIcon.Text;
            hlCancel.NavigateUrl = "EditUser.aspx?tab=memberships&id=" + UserID;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Page.Header.Controls.Add(new LiteralControl("<script type=\"text/javascript\">" +
                                                        DatePickerHelper.GetDatePickerSetup(Localization) +
                                                        "</script>"));
        }

        protected void lbSave_Click(object sender, EventArgs e)
        {
            _presenter.Save();
            if (_presenter.IsValid)
                Response.Redirect("EditUser.aspx?tab=memberships&id=" + UserID);
        }

        public string ContextTranslationFile
        {
            get { return "Admin/Translations/EditMembership.xml"; }
        }

        public int MembershipID
        {
            get
            {
                if (ViewState["MembershipID"] == null)
                    ViewState["MembershipID"] = QueryString.GetInt32ID("id");
                return (int)ViewState["MembershipID"];
            }
        }

        public int UserID
        {
            get
            {
                if (ViewState["UserID"] == null)
                    ViewState["UserID"] = QueryString.GetInt32ID("userid");
                return (int)ViewState["UserID"];
            }
        }

        public void SetUserGroups(IList<UserGroup> groups)
        {
            foreach (UserGroup group in groups)
                ddlUserGroups.Items.Add(new ListItem(group.Name, group.ID.ToString()));
        }

        public int SelectedUserGroupID
        {
            get { return int.Parse(ddlUserGroups.SelectedValue); }
            set { ddlUserGroups.SelectedValue = value.ToString(); }
        }

        public DateTime? StartDate
        {
            get
            {
                if (string.IsNullOrEmpty(txtStartDate.Text.Trim()))
                    return null;
                return DateTime.Parse(txtStartDate.Text);
            }
            set
            {
                txtStartDate.Text = value.HasValue
                                              ? value.Value.ToShortDateString() + " " + value.Value.ToShortTimeString()
                                              : string.Empty;
            }
        }

        public DateTime? EndDate
        {
            get
            {
                if (string.IsNullOrEmpty(txtEndDate.Text.Trim()))
                    return null;
                return DateTime.Parse(txtEndDate.Text);
            }
            set
            {
                txtEndDate.Text = value.HasValue
                                              ? value.Value.ToShortDateString() + " " + value.Value.ToShortTimeString()
                                              : string.Empty;
            }
        }

        public void SetMembershipID(int id)
        {
            ViewState["MembershipID"] = id;
        }
    }
}